<template>
  <div class="update-birthday">
    <van-datetime-picker
        v-model="currentDate"
        type="date"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="$emit('close')"
        @confirm="onComfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
import dayjs from 'dayjs'

export default {
  name: 'UpdateBirthday',
  components: {
  },
  props: {
      value: {
            type: String,
            require: true
      }
  },
  data() {
    return {
        minDate: new Date(1990, 0, 1),
        maxDate: new Date(),
        currentDate: new Date(this.value),
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
        async onComfirm() {
            this.$toast.loading({
                message: '保存中',
                forbidClick: true
            })
            // 原始转化时间
            // const date = `${this.currentDate.getFullYear()}-${this.currentDate.getMonth()}-${this.currentDate.getDate()}`
            const date = dayjs(this.currentDate).format('YYYY-MM-DD')
            await updateUserProfile({
                  birthday: date
              })
            // 返回给父组件 v-model 监听数据变化
            this.$emit('input', date)
            this.$emit('close')
            this.$toast.success('保存成功')
        }
  }
};
</script>
<style lang="less" scoped>
</style>